package ink.onei.user.controller;

import ink.onei.common.annotation.Log;
import ink.onei.common.core.controller.BaseController;
import ink.onei.common.core.domain.AjaxResult;
import ink.onei.common.core.page.TableDataInfo;
import ink.onei.common.enums.BusinessType;
import ink.onei.common.utils.poi.ExcelUtil;
import ink.onei.common.core.domain.entity.Seller;
import ink.onei.user.service.ISellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商家Controller
 *
 * @author nekotako
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/user/seller")
public class SellerController extends BaseController {
    @Autowired
    private ISellerService sellerService;

    /**
     * 查询商家列表
     */
    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @GetMapping("/list")
    public TableDataInfo list(Seller seller) {
        startPage();
        List<Seller> list = sellerService.getSellerList(seller);
        return getDataTable(list);
    }

    /**
     * 导出商家列表
     */
    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @Log(title = "商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Seller seller) {
        List<Seller> list = sellerService.getSellerList(seller);
        ExcelUtil<Seller> util = new ExcelUtil<>(Seller.class);
        util.exportExcel(response, list, "商家数据");
    }

    /**
     * 获取商家详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @GetMapping(value = "/{sellerId}")
    public AjaxResult getInfo(@PathVariable("sellerId") Long sellerId) {
        return success(sellerService.getSellerById(sellerId));
    }

    /**
     * 新增商家
     */
    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @Log(title = "商家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Seller seller) {
        return toAjax(sellerService.insertSeller(seller));
    }

    /**
     * 修改商家
     */
//    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @Log(title = "商家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Seller seller) {
        return toAjax(sellerService.updateSeller(seller));
    }

    /**
     * 删除商家
     */
    @PreAuthorize("@ss.hasPermi('user:seller:all')")
    @Log(title = "商家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sellerIds}")
    public AjaxResult remove(@PathVariable Long[] sellerIds) {
        return toAjax(sellerService.deleteSellerByIds(sellerIds));
    }
}
